import 'package:json_annotation/json_annotation.dart';

part 'song_model.g.dart';

@JsonSerializable()
class SongModel {
  @JsonKey(name: 'id')
  final int id;

  @JsonKey(name: 'name')
  final String name;

  @JsonKey(name: 'artists')
  final List<ArtistModel> artists;

  @JsonKey(name: 'album')
  final AlbumModel album;

  @JsonKey(name: 'duration')
  final int duration;

  @JsonKey(name: 'mp3Url')
  final String? mp3Url;

  SongModel({
    required this.id,
    required this.name,
    required this.artists,
    required this.album,
    required this.duration,
    this.mp3Url,
  });

  factory SongModel.fromJson(Map<String, dynamic> json) => _$SongModelFromJson(json);
  Map<String, dynamic> toJson() => _$SongModelToJson(this);

  // 格式化歌曲时长
  String get formattedDuration {
    int minutes = duration ~/ 1000 ~/ 60;
    int seconds = duration ~/ 1000 % 60;
    return '${minutes.toString().padLeft(2, '0')}:${seconds.toString().padLeft(2, '0')}';
  }

  // 获取歌手名字字符串
  String get artistsName {
    return artists.map((artist) => artist.name).join('/');
  }
}

@JsonSerializable()
class ArtistModel {
  @JsonKey(name: 'id')
  final int id;

  @JsonKey(name: 'name')
  final String name;

  ArtistModel({
    required this.id,
    required this.name,
  });

  factory ArtistModel.fromJson(Map<String, dynamic> json) => _$ArtistModelFromJson(json);
  Map<String, dynamic> toJson() => _$ArtistModelToJson(this);
}

@JsonSerializable()
class AlbumModel {
  @JsonKey(name: 'id')
  final int id;

  @JsonKey(name: 'name')
  final String name;

  @JsonKey(name: 'picUrl')
  final String picUrl;

  AlbumModel({
    required this.id,
    required this.name,
    required this.picUrl,
  });

  factory AlbumModel.fromJson(Map<String, dynamic> json) => _$AlbumModelFromJson(json);
  Map<String, dynamic> toJson() => _$AlbumModelToJson(this);
}